from utils.selenium.perform import WPerform


class BrowserOperationType:
    """
    浏览器操作类型
    """
    max = 1
    scroll = 2
    size = 3
    position = 4
    rect = 5
    back = 6
    forward = 7
    refresh = 8
    close = 9
    quit = 10
    switch = 11


def run_browser_operation(perform: WPerform, item: dict):
    """浏览器操作"""
    browser_operation_type = item.get('browser_operation_type')
    if browser_operation_type == BrowserOperationType.max:
        perform.driver.maximize_window()
    elif browser_operation_type == BrowserOperationType.scroll:  # 滚动
        js = "window.scrollTo(%d,%d)" % (item.get('scroll_x'), item.get('scroll_y'))
        perform.driver.execute_script(js)
    elif browser_operation_type == BrowserOperationType.size:  # 窗口大小
        browser_width = item.get('browser_width')
        browser_height = item.get('browser_height')
        perform.driver.set_window_size(browser_width, browser_height)
    elif browser_operation_type == BrowserOperationType.position:  # 窗口大小
        browser_x = item.get('browser_x')
        browser_y = item.get('browser_y')
        perform.driver.set_window_position(browser_x, browser_y)
    elif browser_operation_type == BrowserOperationType.rect:  # 窗口大小
        browser_x = item.get('browser_x')
        browser_y = item.get('browser_y')
        browser_width = item.get('browser_width')
        browser_height = item.get('browser_height')
        perform.driver.set_window_rect(browser_x, browser_y, browser_width, browser_height)
    elif browser_operation_type == BrowserOperationType.back:  # 回退
        perform.driver.back()
    elif browser_operation_type == BrowserOperationType.forward:  # 前进
        perform.driver.forward()
    elif browser_operation_type == BrowserOperationType.refresh:  # 刷新
        perform.driver.refresh()
    elif browser_operation_type == BrowserOperationType.close:  # 关闭
        perform.driver.close()
    elif browser_operation_type == BrowserOperationType.quit:  # 退出
        perform.driver.quit()
    elif browser_operation_type == BrowserOperationType.switch:  # 切换窗口
        browser_switch_index = item.get('browser_switch_index')
        perform.switch_window(browser_switch_index - 1)